home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 19 advanced win32 techniques / win32techniquesdemo / servicecontrollerform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  8.3 KB  |  208 lines

  1. Imports System.ServiceProcess
  2.  
  3. Public Class ServiceControllerForm
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents txtOut As System.Windows.Forms.TextBox
  28.     Friend WithEvents ServiceController1 As System.ServiceProcess.ServiceController
  29.     Friend WithEvents btnList As System.Windows.Forms.Button
  30.     Friend WithEvents btnQuery As System.Windows.Forms.Button
  31.     Friend WithEvents btnStopIIS As System.Windows.Forms.Button
  32.     Friend WithEvents btnRestartIIS As System.Windows.Forms.Button
  33.  
  34.     'Required by the Windows Form Designer
  35.     Private components As System.ComponentModel.Container
  36.  
  37.     'NOTE: The following procedure is required by the Windows Form Designer
  38.     'It can be modified using the Windows Form Designer.  
  39.     'Do not modify it using the code editor.
  40.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  41.         Me.btnList = New System.Windows.Forms.Button()
  42.         Me.txtOut = New System.Windows.Forms.TextBox()
  43.         Me.ServiceController1 = New System.ServiceProcess.ServiceController()
  44.         Me.btnQuery = New System.Windows.Forms.Button()
  45.         Me.btnStopIIS = New System.Windows.Forms.Button()
  46.         Me.btnRestartIIS = New System.Windows.Forms.Button()
  47.         Me.SuspendLayout()
  48.         '
  49.         'btnList
  50.         '
  51.         Me.btnList.Location = New System.Drawing.Point(8, 16)
  52.         Me.btnList.Name = "btnList"
  53.         Me.btnList.Size = New System.Drawing.Size(120, 40)
  54.         Me.btnList.TabIndex = 1
  55.         Me.btnList.Text = "List services"
  56.         '
  57.         'txtOut
  58.         '
  59.         Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  60.                     Or System.Windows.Forms.AnchorStyles.Left) _
  61.                     Or System.Windows.Forms.AnchorStyles.Right)
  62.         Me.txtOut.Location = New System.Drawing.Point(144, 16)
  63.         Me.txtOut.Multiline = True
  64.         Me.txtOut.Name = "txtOut"
  65.         Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
  66.         Me.txtOut.Size = New System.Drawing.Size(400, 248)
  67.         Me.txtOut.TabIndex = 0
  68.         Me.txtOut.Text = ""
  69.         Me.txtOut.WordWrap = False
  70.         '
  71.         'ServiceController1
  72.         '
  73.         Me.ServiceController1.MachineName = "francesco"
  74.         Me.ServiceController1.ServiceName = "IISADMIN"
  75.         '
  76.         'btnQuery
  77.         '
  78.         Me.btnQuery.Location = New System.Drawing.Point(8, 72)
  79.         Me.btnQuery.Name = "btnQuery"
  80.         Me.btnQuery.Size = New System.Drawing.Size(120, 40)
  81.         Me.btnQuery.TabIndex = 1
  82.         Me.btnQuery.Text = "Query IISAdmin status"
  83.         '
  84.         'btnStopIIS
  85.         '
  86.         Me.btnStopIIS.Location = New System.Drawing.Point(8, 128)
  87.         Me.btnStopIIS.Name = "btnStopIIS"
  88.         Me.btnStopIIS.Size = New System.Drawing.Size(120, 40)
  89.         Me.btnStopIIS.TabIndex = 1
  90.         Me.btnStopIIS.Text = "Stop IISAdmin"
  91.         '
  92.         'btnRestartIIS
  93.         '
  94.         Me.btnRestartIIS.Location = New System.Drawing.Point(8, 184)
  95.         Me.btnRestartIIS.Name = "btnRestartIIS"
  96.         Me.btnRestartIIS.Size = New System.Drawing.Size(120, 40)
  97.         Me.btnRestartIIS.TabIndex = 1
  98.         Me.btnRestartIIS.Text = "Restart IISAdmin"
  99.         '
  100.         'ServiceControllerForm
  101.         '
  102.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  103.         Me.ClientSize = New System.Drawing.Size(560, 277)
  104.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnRestartIIS, Me.btnStopIIS, Me.btnQuery, Me.btnList, Me.txtOut})
  105.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  106.         Me.Name = "ServiceControllerForm"
  107.         Me.Text = "ServiceController component"
  108.         Me.ResumeLayout(False)
  109.  
  110.     End Sub
  111.  
  112. #End Region
  113.  
  114.     Sub LogMessage(ByVal msg As String)
  115.         txtOut.AppendText(msg & ControlChars.CrLf)
  116.     End Sub
  117.  
  118.     ' list all Windows services
  119.  
  120.     Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
  121.         Dim sc As ServiceController
  122.  
  123.         txtOut.Text = ""
  124.         ' List all non-device services on local machines.
  125.         LogMessage("--- NON-DEVICE SERVICES:")
  126.         For Each sc In ServiceController.GetServices
  127.             LogMessage(sc.ServiceName & " (" & sc.DisplayName & ")")
  128.         Next
  129.  
  130.         ' List all device services on local machines.
  131.         LogMessage("")
  132.         LogMessage("--- DEVICE SERVICES:")
  133.         For Each sc In ServiceController.GetDevices
  134.             LogMessage(sc.ServiceName & " (" & sc.DisplayName & ")")
  135.         Next
  136.     End Sub
  137.  
  138.     Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
  139.         ' display information on the IISADMIN service on the local machine
  140.         Dim scIISAdmin As New ServiceController("IISADMIN", ".")
  141.  
  142.         txtOut.Text = ""
  143.         LogMessage("ServiceName = " & scIISAdmin.ServiceName)
  144.         LogMessage("DisplayName = " & scIISAdmin.DisplayName)
  145.         LogMessage("MachineName = " & scIISAdmin.MachineName)
  146.         LogMessage("Status = " & scIISAdmin.Status.ToString)
  147.         LogMessage("CanStop = " & scIISAdmin.CanStop)
  148.         LogMessage("CanPauseAndContinue = " & scIISAdmin.CanPauseAndContinue)
  149.         LogMessage("CanShutDown = " & scIISAdmin.CanShutdown)
  150.         LogMessage("ServiceType = " & scIISAdmin.ServiceType.ToString)
  151.  
  152.         ' List services on which this depends on.
  153.         LogMessage("Services this service depends on:")
  154.         Dim sc As ServiceController
  155.         For Each sc In scIISAdmin.ServicesDependedOn
  156.             LogMessage("  " & sc.ServiceName & " (" & sc.DisplayName & ")")
  157.         Next
  158.  
  159.         ' List services that depend on this service.
  160.         LogMessage("Services that depend on this service:")
  161.         For Each sc In scIISAdmin.DependentServices
  162.             LogMessage("  " & sc.ServiceName & " (" & sc.DisplayName & ")")
  163.         Next
  164.     End Sub
  165.  
  166.     ' stop the IIS service
  167.  
  168.     Private Sub btnStopIIS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopIIS.Click
  169.         ' Get a reference to the IISADMIN service on the local machine
  170.         Dim scIISAdmin As New ServiceController("IISADMIN", ".")
  171.  
  172.         If scIISAdmin.CanStop Then
  173.             scIISAdmin.Stop()
  174.             ' Wait until the service is stopped.
  175.             scIISAdmin.WaitForStatus(ServiceControllerStatus.Stopped)
  176.             LogMessage("The service has stopped")
  177.         Else
  178.             MessageBox.Show("Unable to stop the service at this time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  179.         End If
  180.     End Sub
  181.  
  182.     ' restart the IIS service
  183.  
  184.     Private Sub btnRestartIIS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestartIIS.Click
  185.         ' Get a reference to the IISADMIN service on the local machine
  186.         Dim scIISAdmin As New ServiceController("IISADMIN", ".")
  187.  
  188.         ' Ensure that all the services this service depends on are running.
  189.         Dim sc As ServiceController
  190.         For Each sc In scIISAdmin.ServicesDependedOn
  191.             If sc.Status <> ServiceControllerStatus.Running Then
  192.                 sc.Start()
  193.             End If
  194.         Next
  195.  
  196.         ' Now you can start this service.
  197.         scIISAdmin.Start()
  198.  
  199.         ' Wait until the service is running (timeout = 5 secs)
  200.         scIISAdmin.WaitForStatus(ServiceControllerStatus.Running, New TimeSpan(0, 0, 5))
  201.         If scIISAdmin.Status = ServiceControllerStatus.Running Then
  202.             LogMessage("The service is running")
  203.         Else
  204.             LogMessage("Unable to start the service")
  205.         End If
  206.     End Sub
  207. End Class
  208.